package com.example.bbs.controller;


import com.example.bbs.entity.DiseaseTag;
import com.example.bbs.entity.Selfchecking;
import com.example.bbs.service.DiseaseTagService;
import com.example.bbs.service.SelfcheckingService;
import com.example.bbs.util.SelfcheckingResove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/selfchecking")
public class SelfcheckingController {
    @Autowired
    private SelfcheckingService selfcheckingService;

    @Autowired
    private DiseaseTagService diseaseTagService;

    //进入自检库首页
    @RequestMapping("/index")
    public String defaultIndex(Model model) {
        List<DiseaseTag> diseaseTags = diseaseTagService.selectAllDisease();
        List<Selfchecking> selfcheckings = selectPartsByIds(null);
        model.addAttribute("diseaseTags", diseaseTags);
        model.addAttribute("selfcheckings", selfcheckings);
        return "selfchecking_list";
    }

    //获取 id title description
    @RequestMapping("/selectbyids1")
    @ResponseBody
    public List<Selfchecking> selectPartsByIds(@RequestBody String[] ids) {
        ArrayList<Integer> res = new ArrayList<>();
        if (ids == null || ids.length == 0) {
            return selfcheckingService.selectPartsByIds(res);
        } else {
            for (String id : ids) {
                res.add(Integer.parseInt(id));
            }
            return selfcheckingService.selectPartsByIds(res);
        }
    }


    //进入测试题
    @RequestMapping("/selectOneSelfchecking/{id}")
    public String selectOneSelfchecking(@PathVariable("id") String id, Model model, HttpSession session) {
        SelfcheckingResove selfcheckingResove = selfcheckingService.selectById(Integer.parseInt(id));
        model.addAttribute("selfchecking", selfcheckingResove);
        model.addAttribute("user", session.getAttribute("user"));
        return "selfchecking_detail";
    }


}
